;!function () {
    var form = layui.form, layer = layui.layer;
    form.verify({
        username: [/^[a-zA-Z][a-zA-Z0-9_]{4,15}$/, '用户名为5到16位（字母开始，其他可使用数字，下划线)'],
        password: [/^[\S|\s]{5,20}$/, '密码为5到20位'],
        //重复密码校验
        checkpassword: function (value, item) { //value：表单的值、item：表单的DOM对象
            if ($("#newpassword").val() !== $("#password").val()) {
                return '密码不一致';
            }
        }
    });

    form.on('submit(login)', function (data) {
        layer.msg("网络请求中...");
        myajax("/login", "post", data.field,
            function (result) {
                if (result.status == 200) {
                    window.location.href = "/html/admin.html";
                } else if (result.status == 400) {
                    layer.msg(result.message);
                }
            },
            function () {
                layer.msg("网络请求错误");
            });
        return false;
    });
    form.on('submit(register)', function (data) {
        layer.msg("网络请求中...");
        myajax("/user/checkusername", "get", {username: data.field.username},
            function (result) {
                if (result.status == 200) {
                    myajax("/user/add", 'post', data.field,
                        function (res) {
                            if (res.status == 200) {
                                layer.msg('注册成功');
                                window.location.href = "/html/login.html";
                            } else if (res.status == 400) {
                                layer.msg('参数错误，注册失败');
                            } else if (res.status == 500) {
                                layer.msg('服务器错误，注册失败');
                            }
                        },
                        function () {
                            layer.msg("网络请求错误");
                        });
                }
                if (result.status == 400) {
                    layer.msg("用户名已存在，请更换一个更炫酷的名字");
                }
            },
            function () {
                layer.msg("网络请求错误");
            }
        );
        return false;
    });


}();

